package zhihuilan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import zhihuilan.common.utils.RedisCache;
import zhihuilan.dto.MyCircleDto;
import zhihuilan.dto.MyStaffShopGoodsDto;
import zhihuilan.dto.WxMiniPageDto;
import zhihuilan.entity.MyCircle;
import zhihuilan.entity.MyCircleCollection;
import zhihuilan.entity.MyFactoryFav;
import zhihuilan.entity.MyStaff;
import zhihuilan.mapper.MyCircleCollectionMapper;
import zhihuilan.mapper.MyCircleMapper;
import zhihuilan.mapper.MyFactoryFavMapper;
import zhihuilan.mapper.MyStaffMapper;
import zhihuilan.params.query.MyCircleCollectionQueryParam;
import zhihuilan.params.query.MyFactoryFavQueryParam;
import zhihuilan.service.MyCircleCollectionService;
import zhihuilan.service.MyStaffService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户数据 服务实现
 *
 * @author ruoyi
 */
@Service
public class MyCircleCollectionServiceImpl extends ServiceImpl<MyCircleCollectionMapper, MyCircleCollection> implements MyCircleCollectionService {

    @Resource
    private MyFactoryFavMapper myFactoryFavMapper;

    @Override
    public WxMiniPageDto<MyCircleDto> selectPageList(MyCircleCollectionQueryParam queryParam, MyStaff currentStaff) {
        if (ObjectUtils.isEmpty(queryParam.getStaffId())) {
            return new WxMiniPageDto<MyCircleDto>();
        }
        long pageNum = 0;
        long pageSize = 10;
        if (queryParam.getPage() >= 0L) {
            pageNum = queryParam.getPage();
        }
        if (queryParam.getPageSize() > 0L) {
            pageSize = queryParam.getPageSize();
        }
        if (pageNum > 0) {
            pageNum = pageSize * (pageNum - 1);
        }
        if (!ObjectUtils.isEmpty(queryParam.getDistrictid())) {
            queryParam.setDistrictid1(queryParam.getDistrictid());
        } else {
            if (!ObjectUtils.isEmpty(queryParam.getProvinceid())) {
                queryParam.setDistrictid2(queryParam.getProvinceid());
            }
        }
        List<MyCircleDto> lsit = this.baseMapper.selectPageListToCircle(pageNum, pageSize, queryParam.getStaffId(), queryParam.getDistrictid1(), queryParam.getDistrictid2());
        Long count = this.baseMapper.selectCountToCircle(queryParam.getStaffId(), null, queryParam.getDistrictid1(), queryParam.getDistrictid2());
        WxMiniPageDto<MyCircleDto> result = new WxMiniPageDto(pageNum, pageSize);
        List<MyCircleDto> convertList = lsit.stream().map(po -> {
            if (!ObjectUtils.isEmpty(currentStaff) && !ObjectUtils.isEmpty(currentStaff.getId())) {
                // 自己是否已关注
                QueryWrapper<MyFactoryFav> queryWrapperFav = new QueryWrapper<>();
                queryWrapperFav.eq("staffid", currentStaff.getId());
                queryWrapperFav.eq("fav_staffid", po.getStaffId());
                Long favCount = this.myFactoryFavMapper.selectCount(queryWrapperFav);
                if (ObjectUtils.isEmpty(favCount) || favCount == 0L) {
                    po.setIsfav(0);
                } else {
                    po.setIsfav(1);
                }
            } else {
                po.setIsfav(0);
            }
            return po;
        }).collect(Collectors.toList());
        result.setData(convertList);
        result.setTotal(count);
        return result;
    }

    @Override
    public Long selectCount(MyCircleCollectionQueryParam queryParam) {
        QueryWrapper<MyCircleCollection> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(queryParam)) {
            if (!ObjectUtils.isEmpty(queryParam.getPid())) {
                queryWrapper.eq("pid", queryParam.getPid());
            }
            if (!ObjectUtils.isEmpty(queryParam.getStaffId())) {
                queryWrapper.eq("staff_id", queryParam.getStaffId());
            }
        }
        return this.baseMapper.selectCount(queryWrapper);
    }

    @Override
    public Long selectCountToCricle(MyCircleCollectionQueryParam queryParam) {
        if (ObjectUtils.isEmpty(queryParam)) {
            return 0L;
        }
        if (ObjectUtils.isEmpty(queryParam.getStaffId())) {
            return 0L;
        }
        return this.baseMapper.selectCountToCircle(queryParam.getStaffId(), queryParam.getPid(), null, null);
    }

    @Override
    public MyCircleCollection addEntity(MyCircleCollection entity) {
        entity.setCreateTime(new Date().getTime() / 1000);
        this.baseMapper.insert(entity);
        return entity;
    }

    @Override
    public Boolean deleteById(Integer id) {
        return this.baseMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean deleteByPidAndStaffId(Integer pid, Integer staffId) {
        if (ObjectUtils.isEmpty(pid) || ObjectUtils.isEmpty(staffId)) {
            return false;
        }
        QueryWrapper<MyCircleCollection> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(pid)) {
            queryWrapper.eq("pid", pid);
        }
        if (!ObjectUtils.isEmpty(staffId)) {
            queryWrapper.eq("staff_id", staffId);
        }
        return this.baseMapper.delete(queryWrapper) > 0;
    }
}
